/*
 * \brief  Frontend of input service
 * \author Norman Feske
 * \date   2006-08-16
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__INPUT__COMPONENT_H_
#define _INCLUDE__INPUT__COMPONENT_H_

#include <base/env.h>
#include <os/ram_dataspace.h>
#include <root/component.h>
#include <input_session/server.h>
#include <input/event.h>


/********************
 ** Input back end **
 ********************/

/**
 * Check if an event is pending
 */
int event_pending();


/**
 * Wait for an event, Zzz...zz..
 */
Input::Event get_event();


/*****************************
 ** Input service front end **
 *****************************/

namespace Input {

	class Session_component : public Session_server
	{
		private:

			/*
			 * Input event buffer that is shared with the client
			 */
			enum { MAX_EVENTS = 1000 };

			Genode::Ram_dataspace _ev_ds;

		public:

			Session_component()
			: _ev_ds(Genode::env()->ram_session(), MAX_EVENTS*sizeof(Event)) { }

			Genode::Dataspace_capability dataspace() { return _ev_ds.cap(); }

			bool is_pending() { return event_pending(); }

			int flush()
			{
				/* dump events into event buffer dataspace */
				int i;
				Input::Event *ev_ds_buf = _ev_ds.local_addr<Input::Event>();
				for (i = 0; (i < MAX_EVENTS) && event_pending(); i++)
					ev_ds_buf[i] = get_event();

				/* return number of flushed events */
				return i;
			}
	};


	class Root : public Genode::Root_component<Session_component>
	{
		protected:

			Session_component *_create_session(const char *args) {
				return new (md_alloc()) Session_component(); }

		public:

			Root(Genode::Server_entrypoint *session_ep,
			     Genode::Allocator         *md_alloc)
			: Genode::Root_component<Session_component>(session_ep, md_alloc) { }
	};
}

#endif /* _INCLUDE__INPUT__COMPONENT_H_ */
